package com.wlw.service;

import com.wlw.util.util.GsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl {
    @Autowired
    private RedisTemplate redisTemplate;


    public void set(String key, Object object) {
        redisTemplate.opsForValue().set(key, GsonUtil.toJson(object));
    }


    public void del(String key) {
      Boolean a= redisTemplate.delete(key);
       int i=0;
    }


    public void expire(String key, long seconds) {
        redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
    }
    public void setexpire(String key, long seconds,String value) {
        redisTemplate.opsForValue().set(key, value,seconds, TimeUnit.SECONDS);

    }

    public Object get(String key, Class className) {
        Object object = redisTemplate.opsForValue().get(key);
        if (object == null) {
            return null;
        }

        try {
            return GsonUtil.fromJson(String.valueOf(object), className);
        } catch (Exception e) {
            return null;
        }
    }

    public String getValue(String key) {

        return (String) redisTemplate.opsForValue().get(key);

    }
    public void refreshKeyExpire(String key) {
        redisTemplate.expire(key, 60*60*60, TimeUnit.SECONDS);
    }


    public boolean existKey(String key) {
        return redisTemplate.hasKey(key);

    }

    /**
     * 添加set
     * @param key
     * @param vale
     */
    public  void SetOperations(String key, Set<String> vale){
        redisTemplate.opsForSet().add(key,vale);
    }

    public void setString(String key, String object) {
        redisTemplate.opsForValue().set(key,object);
    }
}
